package pt.isel.lercm.projectmanager.spring.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import pt.isel.lercm.projectmanager.spring.model.GrupoRedmine;

@Repository
public class GrupoRedmineDAO implements IGrupoRedmineDAO{

	@Autowired
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Override
	public void addObject(GrupoRedmine object) {
		getSessionFactory().getCurrentSession().save(object);
	}

	@Override
	public void updateObject(GrupoRedmine object) {
		getSessionFactory().getCurrentSession().update(object);
	}

	@Override
	public void deleteObject(GrupoRedmine object) {
		getSessionFactory().getCurrentSession().delete(object);
	}

	@Override
	public GrupoRedmine getByid(int id) {
		List<GrupoRedmine> list = getSessionFactory().getCurrentSession().createQuery("from GrupoRedmine where id=?").setParameter(0, id).list();
		
		if (list == null || list.isEmpty()){
			return null;
		}
		
		return list.get(0);
	}

	@Override
	public List<GrupoRedmine> getAll() {
		List<GrupoRedmine> list = getSessionFactory().getCurrentSession().createQuery("from GrupoRedmine").list();
		
		if (list == null || list.isEmpty()){
			return null;
		}
		
		return list;
	}

	@Override
	public List<GrupoRedmine> getAllByYear(int idAno) {
		List<GrupoRedmine> list = getSessionFactory().getCurrentSession().createQuery("from GrupoRedmine where ano_id=?").setParameter(0, idAno).list();
		
		if (list == null || list.isEmpty()){
			return null;
		}
		
		return list;
	}

}
